package cn.com.shbs.Controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import cn.com.shbs.Config.Page;
import cn.com.shbs.Dao.Product.ClassProductLevelDao;

@RestController
@RequestMapping("API/")
public class ProductController {
	
	static Logger logger = LoggerFactory.getLogger(NCardServiceController.class);
	
	 @Autowired
	 ClassProductLevelDao classProductLevelDao;
	
	 //查询分类
    @RequestMapping("/selectProductClassify")
    public Object selectProductClassify(HttpServletRequest request,
                                 HttpServletResponse response,
                                 @RequestParam(value="class_code",
                                 required=false)String class_code){

        logger.info("ProductController.selectProductClassify() 方法start");
        String message = "success";
        JSONObject result=new JSONObject();
        List<HashMap> serviceList =new ArrayList<>();
        HashMap<String,Object> map=new HashMap<>();
        try{
            map.put("class_code",class_code);
            serviceList=classProductLevelDao.selectProductClassify(map);
        }catch (Exception e){
            message = "error";
            logger.error("ProductController.selectProductClassify()",e);
            e.printStackTrace();
        }

        logger.info("ProductController.selectProductClassify() 方法end");
        result.put("serviceList", JSONArray.fromObject(serviceList));

        result.put("message",message);
        return result.toString();

    }
    
    
    /**
     * 按要求查询项目
     * @param request
     * @param response
     * @param page
     * @param class_code 分类code
     * @param servicename 服务名称
     * @return
     */
    @RequestMapping("/queryProductClassService")
    public Object queryProductClassService(HttpServletRequest request,
                                    HttpServletResponse response,
                                    Page page,
                                    @RequestParam(value="class_code",required=false)String class_code,
                                    @RequestParam(value="servicename",required=false)String servicename){
        logger.info("queryProductClassService() 方法start");
        String message = "success";
        JSONObject result=new JSONObject();
        HashMap<String,Object> servicemap=new HashMap<>();

        List<HashMap> serviceList =new ArrayList<>();

        //总条数
        int countnum=0;
        try {
            //参数
            servicemap.put("class_code",class_code);
            servicemap.put("servicename",servicename);

            //查询符合条件的总条数
            countnum = classProductLevelDao.queryProductNum(servicemap);
            //总条数
            page.setTotalCount(countnum);

            servicemap.put("startIndex",page.getStartCount());

            servicemap.put("pageSize", page.getPageSize());

            //通过参数map查询顾客list
            serviceList = classProductLevelDao.queryProduct(servicemap);

        }catch (Exception e){
            message = "error";
            logger.error("queryProductClassService()",e);
            e.printStackTrace();
        }
        logger.info("queryProductClassService() 方法end");
        result.put("serviceList", JSONArray.fromObject(serviceList));
        result.put("page", JSONObject.fromObject(page));
        result.put("message",message);
        return result.toString();
    }
}
